home *** CD-ROM | disk | FTP | other *** search
- /* opendir/readdir/closedir/etc - portable directory reader
- * :ts=8 ma=1 bk=0
- *
- * DIR *opendir(name) char *name;
- * prepare to read directory "name".
- * pass the returned value to other members of this function suite.
- * NULL return indicates error.
- *
- * struct FileInfoBlock *readdir(dirp) DIR *dirp;
- * read the next entry in the directory.
- * NULL return indicates error or end of directory;
- * use IoErr() to distinguish.
- *
- * long telldir(dirp) DIR *dirp;
- * get a "place marker" for the current point in the directory.
- *
- * void seekdir(dirp, key) DIR *dirp; long key;
- * return to a previously noted point in the directory.
- *
- * void rewinddir(dirp) DIR *dirp;
- * return to the beginning of the directory.
- *
- * void closedir(dirp) DIR *dirp;
- * finish operations on directory.
- *
- * FREELY DISTRIBUTABLE
- * j w hamilton, 7 nov 86
- */
- #include <exec/types.h>
- #include "dir.h"
- #include <functions.h>
-
- DIR *
- opendir(name)
- char *name;
- {
- register DIR *dirp;
-
- /* allocate the DIR struct
- */
- if (dirp = (DIR *) AllocMem((long) sizeof(DIR), 0L)) {
- /* allocate DIR's FileInfoBlock struct
- */
- if (dirp->dd_fib = (struct FileInfoBlock *) AllocMem((long) sizeof(struct FileInfoBlock), 0L)) {
- /* lock the desired directory
- * if "name" is ".", open current directory
- */
- if (name[0] != '.' || name[1] != '\0')
- dirp->dd_lock = Lock(name, ACCESS_READ);
- else {
- /* obtain a lock on the current directory
- * by chdir'ing to ":"; chdir back; and
- * duplicate the current directory lock
- */
- dirp->dd_lock = CurrentDir(0L);
- CurrentDir(dirp->dd_lock);
- dirp->dd_lock = DupLock(dirp->dd_lock);
- }
- if (dirp->dd_lock) {
- /* if we managed to Lock the directory,
- * there should be no prob Examine'ing it
- */
- if (Examine(dirp->dd_lock, dirp->dd_fib)) {
- /* make sure it's a directory
- */
- if (dirp->dd_fib->fib_DirEntryType > 0L)
- return(dirp);
- }
- /* without errors, we never reach here
- */
- UnLock(dirp->dd_lock);
- }
- FreeMem(dirp->dd_fib, (long) sizeof(struct FileInfoBlock));
- }
- FreeMem(dirp, (long) sizeof(DIR));
- }
- return(NULL);
- }
-
- /* examine the next entry in the directory;
- * return the FIB pointer, or NULL.
- * IoErr() is available for rigor
- */
- struct FileInfoBlock *
- readdir(dirp)
- DIR *dirp;
- {
- if (dirp) {
- if (ExNext(dirp->dd_lock, dirp->dd_fib))
- return(dirp->dd_fib);
- }
- return(NULL);
- }
-
- /* get a "magic cookie" to mark the current position in the directory.
- * use this value for seekdir()
- */
- LONG
- telldir(dirp)
- DIR *dirp;
- {
- if (dirp)
- return(dirp->dd_fib->fib_DiskKey);
- return(NULL);
- }
-
- /* move to a previously noted position in the directory
- */
- void
- seekdir(dirp, key)
- DIR *dirp;
- LONG key;
- {
- if (dirp)
- dirp->dd_fib->fib_DiskKey = key;
- }
-
- /* move back to the beginning of the directory
- */
- void
- rewinddir(dirp)
- DIR *dirp;
- {
- if (dirp)
- Examine(dirp->dd_lock, dirp->dd_fib);
- }
-
- /* "close" the directory, release lock and memory.
- */
- void
- closedir(dirp)
- DIR *dirp;
- {
- if (dirp) {
- UnLock(dirp->dd_lock);
- FreeMem(dirp->dd_fib, (long) sizeof(struct FileInfoBlock));
- FreeMem(dirp, (long) sizeof(DIR));
- }
- }
-